package com.auleev.string;

import java.lang.reflect.Field;
import java.text.MessageFormat;

public class Couple {
  private String nom;
  private String matricule;

  public Couple(String nom, String matricule) {
    super();
    this.nom = nom;
    this.matricule = matricule;
  }

  public String getNom() {
    return nom;
  }

  public void setNom(String nom) {
    this.nom = nom;
  }

  public String getMatricule() {
    return matricule;
  }

  public void setMatricule(String matricule) {
    this.matricule = matricule;
  }
  
  
  public String toString() {
    
      StringBuffer sb = new StringBuffer();
      MessageFormat mf = new MessageFormat("{0} => {1}, ");
      Field[] fields = this.getClass().getDeclaredFields();

      for (Field field : fields) {
          String fieldName = field.getName();
          Object fieldValue = null;
          try {
              fieldValue = field.get(this);
              if (fieldValue.toString().contains("${var2}")){
                fieldValue = fieldValue.toString().replace("${var2}", "Marie");
                field.set(this,fieldValue); 
              }
              
          } catch (IllegalArgumentException e) {
              // Ne rien faire
          } catch (IllegalAccessException e) {
              // Ne rien faire
          }
          mf.format(new Object[] { fieldName, fieldValue }, sb, null);
      }
      if (sb.length() > 1) {
          sb.setLength(sb.length() - 2);
      }

      return sb.toString();
  }
  
  public void replace(){
    
    Field[] fields = this.getClass().getDeclaredFields();

    for (Field field : fields) {
        String fieldName = field.getName();
        Object fieldValue = null;
        try {
            fieldValue = field.get(this);
            if (fieldValue.toString().contains("${var1}")){
              fieldValue = fieldValue.toString().replace("${var1}", "Olivier");
              field.set(this,fieldValue); 
              System.out.println("bonjouree");
            }
            
        } catch (IllegalArgumentException e) {
            // Ne rien faire
        } catch (IllegalAccessException e) {
            // Ne rien faire
        }
    }

    
  }
}
